var ns, app;
app = new Vue({
    el: '#app',
    data: {
        ID:0,
        form: {
            ID: 0,
            PartakeUserList:[],
            TagList:[],
            FileList: [],
            RecordList: [],
        }, 
        IsOpetionMore:false,//是否显示右上角按钮
    },
    methods: {
        get_data: function() {
            var that = this;
            that.showMorePopover=false;//刷新是隐藏更多按钮展示
            var options = {};
            options.ID = that.ID;    
            options.action = "getworktaskdetail";
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form=data.data;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        img_browser: function(url) {
            var that = this;
            var imgurls = [];
            imgurls.push(url);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        onDealTime(time) {
            if(!time){
                return '';
            }
            return moment(time).format('YYYY-MM-DD HH:mm:ss') || '';
        },
        do_complete(){
            var that=this;
            api.confirm({
                title: '提示信息',
                msg: '完成任务后，任务不能进行编辑和添加工作记录等操作。确认要完成任务吗？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        var options={};
                        options.action="completeworktask";
                        options.ID=that.ID;
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    api.toast({
                                        msg: '操作成功',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    api.sendEvent({
                                        name: 'updata_task_detail',
                                    });
                                    api.sendEvent({
                                        name: 'reloadworktasklist',
                                    });
                                    setTimeout(function () {
                                        api.closeWin();
                                    }, 300)
                                    return
                                }
                                else if (data.error) {
                                    api.toast({
                                        msg: data.error,
                                        duration: 3000,
                                        location: 'bottom'
                                    });
                                }
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            })
        },
        do_restart(){
            var that=this;
            api.confirm({
                title: '提示信息',
                msg: '任务重启后可以添加和编辑工作记录。确认要重启该任务吗？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        var options={};
                        options.action="worktaskrestart";
                        options.ID=that.ID;
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                app.get_data();
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            })
        },
        do_release(){
            var that=this;
            api.confirm({
                title: '提示信息',
                msg: '发布任务后，任务状态变更为【进行中】。确认要发布任务吗？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        var options={};
                        options.action="sendworktask";
                        options.ID=that.ID;
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                app.get_data();
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            })
        },
        dochangeuser(){
            var that = this;
            that.IsOpetionMore=false;
            ns.openWin('worktaskchangeuser', '变更人员', {
                ID:that.ID,  
            });
        },
        doworktaskedit(){
            var that = this;
            that.IsOpetionMore=false;
            ns.openWin('worktaskadd', '修改工作任务', {
                ID:that.ID,  
            });
        },
        doworktaskdel(){
            var that=this;
            that.IsOpetionMore=false;
            api.confirm({
                title: '提示信息',
                msg: '任务删除后无法恢复，且其他参与人员也无法查看到该任务。确认要进行删除吗？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        var options={};
                        options.action="deleteworktask";
                        options.ID=that.ID;
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                api.sendEvent({
                                    name: 'reloadworktasklist',
                                });
                                setTimeout(function () {
                                    api.closeWin();
                                }, 300)
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '提交中'
                        });
                    }
                }
            })
        },
        //-------------工作记录----------
         //添加
         doOpenRecordAdd(){
            var that = this;
            ns.openWin('worktaskrecord', '添加工作记录', {
                taskid: that.ID,//任务ID
                id:0,//记录ID
            });
        },
        //修改
        doOpenRecord(list){
            var that = this;
            var RecordID=0;
            if (list) {
                var RecordID = list[0].ID;
                if (list.length > 1) {
                    RecordID = list[list.length - 1].ID;
                }
            }
            ns.openWin('worktaskrecord', '修改工作记录', {
                taskid: that.ID,//任务ID
                id:RecordID,//记录ID
            });
        },
        doDelRecord(list){
            var IDs = list.map((item) => {
                return item.ID
            });
            if (IDs == undefined || IDs.length <= 0) {
                return
            }
            api.confirm({
                title: '提示信息',
                msg: '确认删除工作记录？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        var options={};
                        options.action="deleteworktaskrecord";
                        options.IDs=JSON.stringify(IDs);
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                if(data.status){
                                    app.get_data();
                                    return;
                                }
                                else if (data.error) {
                                    api.toast({
                                        msg: data.error,
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    return;
                                }
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '删除中'
                        });
                    }
                }
            })
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    imageBrowser = api.require('imageBrowser');
    app.ID = api.pageParam.id || 0;

    app.get_data();
    api.addEventListener({
        name: 'updata_task_detail'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
            app.IsOpetionMore = false;
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });

        }
    });

    //控制显示右上角按钮
    //变更人员  编辑  删除
    api.addEventListener({
        name: 'do_change_line'
    }, function (ret, err) {
       app.IsOpetionMore=!app.IsOpetionMore;
    });
};
